package com.example.neusoft.a16110100126yttordering.activities;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.neusoft.a16110100126yttordering.R;
import com.example.neusoft.a16110100126yttordering.bean.Result;
import com.example.neusoft.a16110100126yttordering.listener.RetrofitListener;
import com.example.neusoft.a16110100126yttordering.model.UserModel;

public class RegisterActivity  extends BaseActivity implements RetrofitListener<Result> {
    private EditText et_user,password,repassword,telephone,adress,comment;
    private ImageView imageView2;
    private TextView textView2;
    private Button button5;
    private ImageButton imageButton2;
    private Intent intent;
    private UserModel userModel;

    @Override
    void initViews() {
        setContentView(R.layout.activity_register);
        et_user=findViewById(R.id.editText3);
        password=findViewById(R.id.editText4);
        repassword=findViewById(R.id.editText5);
        telephone=findViewById(R.id.editText6);
        adress=findViewById(R.id.editText7);
        comment=findViewById(R.id.editText);
        imageView2=findViewById(R.id.imageView2);
        textView2=findViewById(R.id.textView2);
        button5=findViewById(R.id.button5);
        imageButton2=findViewById(R.id.imageButton2);
    }

    @Override
    void initEvents() {
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent=new Intent();
//                intent.setClass(RegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
                if(password.getText().toString().equals(repassword.getText().toString())){
                    register();
                }else {
                    showToast("请确认两次密码相同");
                }
            }
        });
        imageButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent();
//                intent.setClass(RegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
                intent.putExtra("user",et_user.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    void initData() {

    }

    private void register(){
        userModel=new UserModel();
        userModel.userRegister(et_user.getText().toString().trim(),
                password.getText().toString().trim(),
                telephone.getText().toString().trim(),
                adress.getText().toString().trim(),
                comment.getText().toString().trim(),this);
    }

    @Override
    public void onSuccess(Result result, int flag) {
        if(result.getSuccess().equals("1")){
            intent=new Intent();
            intent.putExtra("user",et_user.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }else {
            showToast("注册失败");
        }
    }

    @Override
    public void onFail() {
        showToast("网络发生错误");
    }
}
